/* Copyright (c) 2010 OFXKit
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "OFXInvestment401kStatementRequest.h"

@implementation OFXInvestment401kStatementRequest

@synthesize include401kInformation;
@synthesize include401kBalance;

- (NSString*)formOFX1Request
{
  // raise an exception 401(k) statements are not valid in OFX 1.x
  NSException *exception = [NSException exceptionWithName:@"UnsupportedOption"
                                                   reason:@"OFX 1.x does not support 401(k) requests"
                                                 userInfo:nil];
  @throw exception;
  
  return nil;
}

- (NSString*)formOFX2Request
{
  NSString* request = [super formOFX2Request];
  
  // remove the closing statement
  NSString* closingTag = [NSString stringWithFormat:@"</%@STMTRQ>", accountPrefix];
  NSRange start = [request rangeOfString:closingTag];
  request = [request substringToIndex:start.location];
  
  // add 401k information
  NSString* inc401kinfo = @"N";
  if (include401kInformation) inc401kinfo = @"Y";
  request = [NSString stringWithFormat:@"%@<INC401K>%@</INC401K>", 
             request, inc401kinfo];
  
  // add 401k balance
  NSString* inc401kbal = @"N";
  if (include401kBalance) inc401kbal = @"Y";
  request = [NSString stringWithFormat:@"%@<INC401KBAL>%@</INC401KBAL></%@STMTRQ>", 
             request, inc401kbal, accountPrefix];
  
  return request;
}

@end
